<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pokes extends MY_Controller {

    public function __construct() {
      parent::__construct();
      
      $this->load->model('users_mod');
      $this->load->model('pokes_mod');
      $this->load->model('images_mod');
    }
	/**
	 *
	 */
	public function index()
	{       
        $data['title'] = lang('pokes_title');
        $data['keywords'] = lang('pokes_keywords');
        $data['description'] = lang('pokes_description');
        try{
          $id = $this->session->userdata('user_id');
          $data['user'] = $this->users_mod->getUserData($id,false);
          $data['main_image'] = $this->images_mod->getMainImage($id);
          $data['pokes'] = $this->pokes_mod->getMyPokesList($id);
        } 
        catch( Exception $e )
        {
          $data['user'] = NULL;
          $data['main_image'] = NULL;
          $data['pokes'] = NULL;
        }
        
        $data['menu_active'] = "";//"profile";        
        $data['contents']  = "pokes/home";   
		$this->load->view('template/main',$data);
	}
    
    
    /**
     *  AJAX: send_poke
     *   
     */
    public function send_poke()
    {
        try{
          $poke_id = $this->input->post('poke_id'); // user poke id   
          $user_id = $this->input->post('user_id'); // user poke id  
          $ret =  $this->pokes_mod->send_poke($poke_id); 
          if( 1 == $ret ) 
          {     
            $data['pokes'] = $this->pokes_mod->getMyPokesList($user_id);
            $html = $this->load->view('pokes/pokes_list',$data,true);
            $response = array(
                        'ind' => 1,
                        'msg' => lang('poke_sent'),
                        'html' => $html
                    );
          }
          else if( -2 == $ret)
          {
            $response = array(
                'ind' => -2,
                'msg' => sprintf(lang('user_no_coins'),base_url()."payment.htm")
            );
          }
          else
          {
            $response = array(
                        'ind' => 0,
                        'msg' => lang('poke_not_sent')
                    );
          }
        }
        catch (Exception $e) {
            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        
        echo json_encode($response);
    }
    
    /**
     *  AJAX: remove_favorite
     *  Brisanje user-a iz favorites liste
     */
    public function remove_poke()
    {
        try{
          $poke_id = $this->input->post('id'); // Favorite id        
          $user_id = $this->session->userdata('user_id');          
          if( true == $this->pokes_mod->remove_poke($poke_id) ) 
          {            
            $data['pokes'] = $this->pokes_mod->getMyPokesList($user_id);
            $html = $this->load->view('pokes/pokes_list',$data,true);
            $response = array(
                        'ind' => 1,
                        'msg' => lang('poke_removed'),
                        'html' => $html
                    );
          }
          else
          {
            $response = array(
                        'ind' => 0,
                        'msg' => lang('poke_not_removed')
                    );
          }
        }
        catch (Exception $e) {
            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        
        echo json_encode($response);
    }
    
    
} // end class Pokes
/* End of file pokes.php */
/* Location: ./application/controllers/pokes.php */